package com.zhongketech.sunflower.activity.me;

import java.util.Calendar;

import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.activity.BaseActivity;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.model.User;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyBirthDayActivity extends BaseActivity implements OnClickListener{

	private TextView birthdayTextView;
	public final static String TAG= "ModifyBirthdayActivity";
	private 		Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_birthday);
		handler = new Handler() {
			public void handleMessage(Message msg) {
				finish();
			}
		};
		initView();
		setUserInfo();
	}

	private void setUserInfo() {
		String birthday = getIntent().getExtras().getString("birthday");
		birthdayTextView.setText(birthday);
	}

	private void initView() {
		//		TextView back = (TextView) findViewById(R.id.back);
		TextView title = (TextView) findViewById(R.id.title);
		TextView other = (TextView) findViewById(R.id.other);

		birthdayTextView =(TextView) findViewById(R.id.birthday_text);


		String birthday = getIntent().getExtras().getString("birthday");
		birthdayTextView.setHint(birthday);

		findViewById(R.id.btn).setOnClickListener(this);
		Resources rs = getResources();
		//		back.setOnClickListener(this);
		title.setText("修改出生日期");
		other.setText(rs.getString(R.string.save));
		other.setVisibility(View.VISIBLE);
		other.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.other:
			updateUserInfo();
			break;
		case R.id.btn:
			chooseDate();
			break;
		default:
			break;
		}
	}

	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {	
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
	}

	protected void updateUserInfo() {
		final ProgressDialog pd = new ProgressDialog(this);
		pd.setCanceledOnTouchOutside(false);
		pd.setMessage("正在修改...");
		pd.show();

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();

				User tmpUser = MyApplication.getInstance().getUser();
				tmpUser.setBirthday(birthdayTextView.getText().toString().trim());
				User newUser = new Action().ModifyMe(tmpUser);
				if(null != newUser)
				{
					final Message msg = new Message();
					handler.sendMessage(msg);
					newUser.setPassword(tmpUser.getPassword());
					MyApplication.getInstance().SetUser(newUser);
					MyToast.getInstance().show("更新成功");
				}
			}
		}).start();

	}

	/**
	 * 选择日期
	 * 
	 * @param view
	 */
	public void chooseDate() {
		Calendar calendar = Calendar.getInstance();
		DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker datePicker, int year, int month,
					int dayOfMonth) {
				String mBirthday = year + "-" + (month + 1) + "-" + dayOfMonth;
				birthdayTextView.setText(mBirthday);
			}
		};
		DatePickerDialog dialog = new DatePickerDialog(
				ModifyBirthDayActivity.this, dateListener,
				calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
		dialog.show();
	}
}
